﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Xml.Serialization;

namespace FastNET.CleanAct.Common.Configuration.Files.PackageExport
{
    [Serializable]
    [XmlType(AnonymousType = true)]
    [XmlRoot(Namespace = "", IsNullable = false, ElementName = "root")]
    public class ExportPackageSettings
    {
        public ExportPackageSettings()
        {
            Templates = new List<TemplateItem>();
            PackageInfo = new PackageItem();
        }

        [XmlElement(ElementName = "packageInfo")]
        public PackageItem PackageInfo { get; set; }

        [XmlElement(ElementName = "template")]
        public List<TemplateItem> Templates { get; set; }

        #region Loading/Saving

        /// <summary>
        /// Loads a ExportPackageSettings from the specified file
        /// </summary>
        /// <param name="file">File to load the config from</param>
        /// <returns>ExportPackageSettings with the XML deserialized classes</returns>
        public static ExportPackageSettings Load(string file)
        {
            XmlSerializer s = new XmlSerializer(typeof(ExportPackageSettings));

            using (FileStream fileStream = new FileStream(file, FileMode.Open))
            {
                return (ExportPackageSettings)s.Deserialize(fileStream);
            }
        }

        /// <summary>
        /// Loads a ExportPackageSettings from the specified XML Content
        /// </summary>
        /// <param name="content">XML to load the config from</param>
        /// <returns>ExportPackageSettings with the XML deserialized classes</returns>
        public static ExportPackageSettings LoadXml(string content)
        {
            XmlSerializer s = new XmlSerializer(typeof(ExportPackageSettings));

            using (TextReader reader = new StringReader(content))
            {
                return (ExportPackageSettings)s.Deserialize(reader);
            }
        }

        /// <summary>
        /// Writes a ExportPackageSettings to a string of XML
        /// </summary>
        /// <param name="config">ExportPackageSettings to save</param>
        /// <returns>XML representation of the ExportPackageSettings</returns>
        public static string WriteXML(ExportPackageSettings config)
        {
            XmlSerializer s = new XmlSerializer(typeof(ExportPackageSettings));
            StringBuilder returnValue = new StringBuilder();

            using (TextWriter writer = new StringWriter(returnValue))
            {
                s.Serialize(writer, config);
                return returnValue.ToString();
            }
        }

        /// <summary>
        /// Saves a ExportPackageSettings to the specified file
        /// </summary>
        /// <param name="file">File to save the config to</param>
        /// <param name="config">ExportPackageSettings to save</param>
        public static void Save(string file, ExportPackageSettings config)
        {
            XmlSerializer s = new XmlSerializer(typeof(ExportPackageSettings));

            using (FileStream fileStream = new FileStream(file, FileMode.OpenOrCreate))
            {
                s.Serialize(fileStream, config);
            }
        }

        /// <summary>
        /// Saves a ExportPackageSettings to the specified file
        /// </summary>
        /// <param name="file">File to save the config to</param>
        public void Save(string file)
        {
            Save(file, this);
        }

        #endregion

    }
}